﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Com.AlbLab.GEP.Domain;
using common;

using common.pages;
using Com.AlbLab.GEP.Domain.Entities;
using Com.AlbLab.CR.Stampe;

namespace Com.AlbLab.CR.Web.pages
{
    public partial class RicercaPratica : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                ddlFornitore.DataSource = BasePage.Services.AnagraficheService.getListaVenditori().Values;
                ddlFornitore.DataValueField = "Id_ven_cod_contr_disp";
                ddlFornitore.DataTextField = "Ven_ragsoc";
                ddlFornitore.DataBind();
                ddlFornitore.SelectedValue = "NaN";
                ddlTipoPratica.DataSource = BasePage.Services.AnagraficheService.getListaServizi().Values;
                ddlTipoPratica.DataValueField = "Id_ser_cod_servizio";
                ddlTipoPratica.DataTextField = "Ser_des_servizio_ext";
                ddlTipoPratica.DataBind();
                //ddlTipoPratica.SelectedValue = "NaN";
                ddlTipoPratica.SelectedValue = " ";
                ddlStatoPratica.DataSource = BasePage.Services.AnagraficheService.getStatiPratica().Values;
                ddlStatoPratica.DataValueField = "ID_stati_pratica";
                ddlStatoPratica.DataTextField = "DES_stato_pratica";
                ddlStatoPratica.DataBind();
                ddlUfficioAss.DataSource = BasePage.Services.AnagraficheService.getUffici().Values;
                ddlUfficioAss.DataValueField = "Id_cod_ufficio";
                ddlUfficioAss.DataTextField = "Des_ufficio";
                ddlUfficioAss.DataBind();

            }
        }

        protected void Page_DataBind(object sender, EventArgs e)
        {
          
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string cData1;
                string cData2;
                string cData3;
                string cData4;
                string cData5;
                string cData6;
                string cData7;
                DateTime dData1;
                DateTime dData2;
                DateTime dData3;
                DateTime dData4;
                DateTime dData5;
                DateTime dData6;
                DateTime dData7;

                if (DateTime.TryParse(txtArrDa.Text.ToString(), out dData1) && DateTime.TryParse(txtArrA.Text.ToString(), out dData2))
                {
                    if (dData1 > dData2)
                    {
                        throw new ManagedException("Data arrivo Da successiva Data arrivo A", "EX-RIC004", "", "", "", "", "", null);
                    }
                    else
                    {
                        if (dData1 == dData2)
                        {
                            dData2 = dData2.AddDays(1);
                        }
                        cData1 = dData1.ToString("dd/MM/yyyy");
                        cData2 = dData2.ToString("dd/MM/yyyy");
                    }
                }
                else
                {
                    cData1 = "";
                    cData2 = "";
                }

                if (DateTime.TryParse(txtInsDa.Text.ToString(), out dData3) && DateTime.TryParse(txtInsA.Text.ToString(), out dData4))
                {
                    if (dData3 > dData4)
                    {
                        throw new ManagedException("Data inserimento Da successiva Data inserimento A", "EX-RIC005", "", "", "", "", "", null);
                    }
                    else
                    {
                        if (dData3 == dData4)
                        {
                            dData4 = dData4.AddDays(1);
                        }
                        cData3 = dData3.ToString("dd/MM/yyyy");
                        cData4 = dData4.ToString("dd/MM/yyyy");
                    }
                }
                else
                {
                    cData3 = "";
                    cData4 = "";
                }

                if (DateTime.TryParse(dtaApp.Text.ToString(), out dData5) )
                {
                    cData5 = dData5.ToString("dd/MM/yyyy");
                }
                else
                {
                    if (dtaApp.Text.ToString() !="")
                    {
                        throw new ManagedException("Data appuntamento non corretta", "EX-RIC001", "", "", "", "", "", null);
                    }
                    cData5 = "";
                }

                if (DateTime.TryParse(dtaAttoRich.Text.ToString(), out dData6))
                {
                    cData6= dData6.ToString("dd/MM/yyyy");
                }
                else
                {
                    if (dtaAttoRich.Text.ToString() != "")
                    {
                        throw new ManagedException("Data atto richiesto non corretta", "EX-RIC002", "", "", "", "", "", null);
                    }
                    cData6 = "";
                }

                if (DateTime.TryParse(dtaSosp.Text.ToString(), out dData7))
                {
                    cData7 = dData7.ToString("dd/MM/yyyy");
                }
                else
                {
                    if (dtaSosp.Text.ToString() != "")
                    {
                        throw new ManagedException("Data sospensione non corretta", "EX-RIC003", "", "", "", "", "", null);
                    }
                    cData7 = "";
                }
                

                //List<GEP_TESTATA> searchResult = Services.AnagraficheService.ricercaPratiche(ddlTipoPratica.SelectedValue, "DP1616" ddlFornitore.SelectedValue, txtPiva.Text, txtRagsoc.Text, txtDa.Text, txtA.Text);
                List<GEP_TESTATA> searchResult = Services.AnagraficheService.ricercaPratiche(ddlTipoPratica.SelectedValue, ddlFornitore.SelectedValue, txtPiva.Text, txtPod.Text, txtPrtASM.Text, txtPrtDIS.Text, cData1, cData2, cData3, cData4, Convert.ToInt32(ddlStatoPratica.SelectedValue), Convert.ToInt32(ddlUfficioAss.SelectedValue),cData5,cData6,cData7);
              
                    GrigliaGenerica1.NumColonne = 9;
                    GrigliaGenerica1.H0 = "Pratica ASM";
                    GrigliaGenerica1.H1 = "Pratica Utente";
                    GrigliaGenerica1.H2 = "Cod. Servizio";
                    GrigliaGenerica1.H3 = "Stato pratica";
                    GrigliaGenerica1.H4 = "Ultima Fase";
                    GrigliaGenerica1.H5 = "POD";
                    GrigliaGenerica1.H6 = "Ufficio assegnazione";
                    GrigliaGenerica1.H7 = "Distributore";
                    //IList<ConfAziendaEntity> searchResult = null;
                    //searchResult = null;// base.ServiceConfAzienda().LoadConferimentoByPartialRagSoc(textFilter.Text.Trim(), false);
                    GrigliaGenerica1.DataSource = common.ToGenericListTransformers.transform(Services, searchResult);
                    GrigliaGenerica1.Visible = true;
                    C1.Visible = true;

                    Session.Add("ListaPratiche", searchResult);
                    
            }
            catch (ManagedException e0x)
            {
                ((BasePage)this.Page).info.AddMessage(e0x);
            }
        }

        protected void GrigliaGenerica1_OnSelect(string commandName, GenericListDTO row)
        {
            int nPratica;
            GEP_TESTATA pratica;

            nPratica = Convert.ToInt32(row.Id);
            pratica = null;
            foreach(GEP_TESTATA p in (List<GEP_TESTATA>)Session["ListaPratiche"])
            {
            if (p.Id_tes_cod_pratica == nPratica)
            {
                pratica = p;
            }
            }

            GrigliaGenerica1.Clear();
            common.TempSession.setBag("CODICE_PRATICA", row.Id.Split('/')[0]);

            if (Convert.ToInt32( pratica.Tes_Stato) > 5)
            {
                Response.Redirect("Pratica.aspx");
            }
            else
            {
                switch (pratica.Tes_Cod_Servizio)
                {
                    case "D01":
                        Response.Redirect("~/Pratiche/PaginePratiche/PST_D01.aspx?KAP=2");
                        break;
                    case "M01":
                        Response.Redirect("~/Pratiche/PaginePratiche/PST_M01.aspx?KAP=2");
                        break;
                    case "M02":
                        Response.Redirect("~/Pratiche/PaginePratiche/PST_M02.aspx?KAP=2");
                        break;
                    case "R01":
                        Response.Redirect("~/Pratiche/PaginePratiche/PST_R01.aspx?KAP=2");
                        break;
                    case "V01":
                        Response.Redirect("~/Pratiche/PaginePratiche/PST_V01.aspx?KAP=2");
                        break;
                    case "V02":
                        Response.Redirect("~/Pratiche/PaginePratiche/PST_V02.aspx?KAP=2");
                        break;
                    case "AP0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_AP0.aspx?KAP=2");
                        break;
                    case "CA0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_CA0.aspx?KAP=2");
                        break;
                    case "CF0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_CF0.aspx?KAP=2");
                        break;
                    case "CT0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_CT0.aspx?KAP=2");
                        break;
                    case "EL0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_EL0.aspx?KAP=2");
                        break;
                    case "EL1":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_EL1.aspx?KAP=2");
                        break;
                    case "PR0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_PR0.aspx?KAP=2");
                        break;
                    case "RE0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_RE0.aspx?KAP=2");
                        break;
                    case "RE1":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_RE1.aspx?KAP=2");
                        break;
                    case "RF0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_RF0.aspx?KAP=2");
                        break;
                    case "SB0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_SB0.aspx?KAP=2");
                        break;
                    case "SG0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_SG0.aspx?KAP=2");
                        break;
                    case "SG1":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_SG1.aspx?KAP=2");
                        break;
                    case "SI0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_SI0.aspx?KAP=2");
                        break;
                    case "SM0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_SM0.aspx?KAP=2");
                        break;
                    case "SW0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_SW0.aspx?KAP=2");
                        break;
                    case "SW1":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_SW1.aspx?KAP=2");
                        break;
                    case "VA0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_VA0.aspx?KAP=2");
                        break;
                    case "VP0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_VP0.aspx?KAP=2");
                        break;
                    case "VT0":
                        Response.Redirect("~/Pratiche/PaginePratiche/PNS_VT0.aspx?KAP=2");
                        break;
                }
            }
        }

        
    }
}